package com.labraff.todo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class CreateTask extends AppCompatActivity {
    EditText et_title, et_detail;
    Switch sw_urgent;
    Button btn_newTask;
    task newTask;
    globalApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_task);
        app = ((globalApplication) getApplication());

        // 视图绑定初始化
        et_title = findViewById(R.id.et_title);
        et_detail = findViewById(R.id.et_detail);
        sw_urgent = findViewById(R.id.sw_urgent);
        btn_newTask = findViewById(R.id.btn_newTask);

        // 基础初始化
        init();


    }

    void init() {
        newTask = new task("", false, "", "");
        sw_urgent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    newTask.setIsUrgent(true);
                } else {
                    newTask.setIsUrgent(false);
                }
            }
        });
        btn_newTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!taskFillCheck()) {
                    Toast.makeText(CreateTask.this, "未填写事项", Toast.LENGTH_SHORT).show();
                } else {
                    newTask.setTitle(et_title.getText().toString());
                    newTask.setDetails(et_detail.getText().toString());
                    app.addTask(newTask);
                    // TODO 标签功能
                    Intent intent = new Intent(CreateTask.this, MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    boolean taskFillCheck() {
        if (et_title.getText().toString().length() == 0) return false;
        return true;
    }

}